package com.lonely.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.lonely.common.exception.BusinessException;
import com.lonely.system.engine.upload.FileUploadFactory;
import com.lonely.system.engine.upload.UploadTypeEnum;
import com.lonely.system.service.IFileUploadService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author : 黄志标
 * @date: 2025/6/27 13:04
 * @description: 上传接口实现
 */
@Service
@Slf4j
public class FileUploadServiceImpl implements IFileUploadService {


    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @Override
    public String uploadFile(MultipartFile file) {
        String oldFileName = file.getOriginalFilename();
        Assert.isTrue(StrUtil.isNotBlank(oldFileName), "生成的文件的文件名为空");

        try {
            // 暂时使用 picgo实现，因为免费
            return FileUploadFactory.getFileUpload(UploadTypeEnum.MINIO).uploadFile(file);
        } catch (Exception e) {
            log.error("上传图片失败，异常原因：{}", ExceptionUtils.getStackTrace(e));
            throw new BusinessException("上传图片失败");
        }
    }
}
